<?php

include_once './db_bb_connect.php';

foreach ($_POST as $key => $value) {
      if ($value == '') {
        $value = ":null";
        $$key = $value;
      } else {
        	$$key = $value;
      }
    }

if ($quarter == '1') {

	$sql = "INSERT INTO stats_game_startingfive (playerID, season, gamenumber, s5) 
	VALUES ('$playerID', '$season', $gamenumber,'$S5')";
	
	if ( $db->query($sql) ) {  

    Echo "<center>Starting 5 stats succesfully added </center>";

	} else {
	    echo "Error: " . $sql . "<br>" . $db->error;
	}  
} 


$MPsplit = explode(":", $MP);
$MP = 60 * (int)$MPsplit[0] + (int)$MPsplit[1];

$sql =    "INSERT INTO stats_game_quarter (playerID, season, gamenumber, quarter, ORB, DRB, TOV, ST, FTscored, FTtotal, FGIscored, FGItotal, PF, FGOscored, FGOtotal, THPscored, THPtotal, MP, AST, BS)
VALUES ('$playerID', '$season', $gamenumber, '$quarter', $ORB, $DRB, $TOV, $ST, $FTscored, $FTtotal, $FGIscored, $FGItotal, $PF, $FGOscored , $FGOtotal, $THPscored, $THPtotal, $MP, $AST, $BS)";

try{
      $dbpdo = new PDO('mysql:host=localhost;dbname=basketbal','cas','cas');
      $dbpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $stmt = $dbpdo->prepare($sql);
  	  $stmt->bindValue(":null", 0, PDO::PARAM_INT);
  	  if (!$stmt->execute()) {
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
  	  } 

  	  $sql2 = "SELECT firstname
  		FROM users
   		WHERE user_key = '$playerID'";

  		$result2=$db->query($sql2);
  		$row2=$result2->fetch_assoc();
  		$firstname = $row2['firstname'];
   		echo "<center>Quarter $quarter stats of $firstname are succesfully added to game $gamenumber of season $season </center>";
  		
    } catch (PDOException $e) {    
      $error = $e->getMessage();
      echo $error;
    }


?>

